<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="物料代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
              <j-popup
                v-model="model.materialCode"
                field="materialCode"
                org-fields="material_code,material_name,material_sap,material_unit_name,per_number,warehouse_name,id"
                dest-fields="materialCode,materialName,sap,unit,perNumber,warehouseName,productId"
                code="purchaseinputdetail_material_select"
                :multi="false"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="true">
            <a-form-model-item label="物料id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productId">
              <a-input v-model="model.productId" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单箱数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perNumber">
              <a-input-number v-model="model.perNumber" placeholder="请输入单箱数" style="width: 100%" disabled="" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="箱数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input-number v-model="model.number" placeholder="请输入箱数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalNumber">
              <a-input-number v-model="model.totalNumber" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="收料仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouseName">
              <j-search-select-tag v-model="model.warehouseName" dict="hf_warehouse,warehouse_name,id" />
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="物料库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouseMaterial">-->
          <!--              <a-input v-model="warehouseMaterial" disabled />-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12" v-if="false">
            <a-form-model-item label="所属入库单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseinputId">
              <a-input v-model="model.purchaseinputId" placeholder="请输入所属入库单"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: 'HfPurchaseinputDetailModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {
        number: null,
        totalNumber: null,
        perNumber: null
      },
      warehouseMaterial: 0,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      mainId: '',
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/purchase/hfPurchaseInput/addHfPurchaseinputDetail',
        edit: '/purchase/hfPurchaseInput/editHfPurchaseinputDetail'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  watch: {
    'model.number': {
      deep: true,
      immediate: true,
      handler(newValue) {
        if (this.model.perNumber === null) {
          this.model.totalNumber = this.model.number
        } else {
          this.model.totalNumber = Math.round(this.model.perNumber * this.model.number)
        }
      }
    }
  },
  methods: {
    add(id) {
      this.edit(this.modelDefault, id)
    },
    edit(record, id) {
      this.mainId = id
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model['purchaseinputId'] = this.mainId
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row)
    }
  }
}
</script>
